import request from "@/utils/request";
import { Local } from "@/utils/storage";
import { Notify } from "vant";
//返回结果的时候记得异步获取，调用函数也要异步
const groupContacts = '/group-contacts';
export async function createGroup(groupInfo) {
    try {
        const res = await request.post(groupContacts + '/create', groupInfo);
        if (res.code === 2000) {
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function getGroupList() {
    try {
        const res = await request.get(groupContacts + '/show-all');
        return res.data// 返回 res.data.length
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export async function showGroupAdd() {
    try {
        const res = await request.get(groupContacts + '/show-add');
        Local.set('groupAdd', res.data);
        console.log(res.data);
        if (res.data === null) return 0;
        return res.data.length;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function acceptAdd(groupId, memberId) {
    try {
        const res = await request.get(groupContacts + '/accept',
            {
                params: {
                    groupId: groupId,
                    memberId: memberId
                }
            });
        Notify({ type: "success", message: res.data });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function refuseAdd(groupId, memberId) {
    try {
        const res = await request.get(groupContacts + '/refuse',
            {
                params: {
                    groupId: groupId,
                    memberId: memberId
                }
            });
        Notify({ type: "success", message: res.data });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export async function searchGroup(searchInfo) {
    try {
        const res = await request.get(groupContacts + '/search', {
            params: {
                groupName: searchInfo
            }
        });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function addGroup(addInfo) {
    try {
        const res = await request.post(groupContacts + '/add', addInfo);
        if (res.code === 2000) {
            Notify({ type: 'success', message: res.data });
        }
        else {
            Notify({ type: 'danger', message: res.data });
        }
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function showMembers(groupId) {
    try {
        const res = await request.get(groupContacts + '/show-members', {
            params: {
                groupId: groupId
            }
        });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}

export async function quitGroup(groupId) {
    try {
        const res = await request.get(groupContacts + '/quit', {
            params: {
                groupId: groupId
            }
        });
        Notify({ type: "success", message: res.data });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export  function getNickname(groupId) {
    request.get(groupContacts + '/get-nickname', {
        params: {
            groupId: groupId
        }
    }).then(res => {
        Local.set('nickname', res.data);
        return res.data;
    }).catch(error => {
        return error;
    });
}
export async function resetNickname(groupId,nickname){
    try {
        const res = await request.get(groupContacts + '/reset-nickname', {
            params: {
                groupId: groupId,
                nickname: nickname
            }
        });
        Notify({ type: "success", message: res.data });
        return res.data;
    } catch (error) {
        console.log(error);
        throw error; // 重新抛出错误
    }
}
export function getNumber(groupId){
    request.get(groupContacts + '/get-number', {
        params: {
            groupId: groupId
        }
    }).then(res => {
        Local.set('group-number', res.data);
        return res.data;
    }).catch(error => {
        return error;
    });
}